package com.kaifamiao.high.thread;

import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author lucien-ma
 * @date 2021/10/9
 * @apiNote
 */
public class Demo07 {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        System.out.println("当前时间：" + LocalTime.now());
        for (int i = 0; i < 3; i++) {
            int number = i;
            scheduledExecutorService.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("当前时间：" + LocalTime.now() +
                            ", 线程：" + Thread.currentThread().getName() +
                            ", 序号：" + number);
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, 3, TimeUnit.SECONDS);
        }

        // 执行完任务之后关闭线程
        scheduledExecutorService.shutdown();
    }
}
